package com.dddpeter.app.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dddpeter.app.entity.SysDictData;
import com.dddpeter.app.entity.SysDictType;
import com.dddpeter.app.repository.SysDictDataMapper;
import com.dddpeter.app.repository.SysDictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDictService {
    
    @Autowired
    private SysDictTypeMapper dictTypeMapper;
    
    @Autowired
    private SysDictDataMapper dictDataMapper;
    
    // ========== 字典类型 ==========
    
    /**
     * 获取所有字典类型
     */
    public List<SysDictType> getAllDictTypes() {
        return dictTypeMapper.selectList(null);
    }
    
    /**
     * 根据字典类型查询
     */
    public SysDictType getDictTypeByType(String dictType) {
        LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictType::getDictType, dictType);
        return dictTypeMapper.selectOne(wrapper);
    }
    
    /**
     * 创建字典类型
     */
    public SysDictType createDictType(SysDictType dictType) {
        dictTypeMapper.insert(dictType);
        return dictType;
    }
    
    /**
     * 更新字典类型
     */
    public SysDictType updateDictType(Long dictId, SysDictType dictType) {
        dictType.setDictId(dictId);
        dictTypeMapper.updateById(dictType);
        return dictType;
    }
    
    /**
     * 删除字典类型
     */
    public void deleteDictType(Long dictId) {
        dictTypeMapper.deleteById(dictId);
    }
    
    // ========== 字典数据 ==========
    
    /**
     * 根据字典类型获取字典数据列表
     */
    public List<SysDictData> getDictDataByType(String dictType) {
        LambdaQueryWrapper<SysDictData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictData::getDictType, dictType)
               .eq(SysDictData::getStatus, "0")  // 只查询正常状态
               .orderByAsc(SysDictData::getDictSort);
        return dictDataMapper.selectList(wrapper);
    }
    
    /**
     * 获取所有字典数据
     */
    public List<SysDictData> getAllDictData() {
        return dictDataMapper.selectList(null);
    }
    
    /**
     * 根据ID获取字典数据
     */
    public SysDictData getDictDataById(Long dictCode) {
        return dictDataMapper.selectById(dictCode);
    }
    
    /**
     * 创建字典数据
     */
    public SysDictData createDictData(SysDictData dictData) {
        dictDataMapper.insert(dictData);
        return dictData;
    }
    
    /**
     * 更新字典数据
     */
    public SysDictData updateDictData(Long dictCode, SysDictData dictData) {
        dictData.setDictCode(dictCode);
        dictDataMapper.updateById(dictData);
        return dictData;
    }
    
    /**
     * 删除字典数据
     */
    public void deleteDictData(Long dictCode) {
        dictDataMapper.deleteById(dictCode);
    }
    
    /**
     * 获取文章分类列表（常用方法）
     */
    public List<SysDictData> getArticleCategories() {
        return getDictDataByType("article_category");
    }
}

